package gofish.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.BatchPutAttributesRequest;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.ReplaceableItem;
import com.amazonaws.services.simpledb.model.SelectRequest;

public class GoFishDAO {
	
	private AmazonSimpleDB sdb;

	public void connect() throws DAOException {
		String propFile = "etc/AwsCredentials.properties";
		try {
			sdb = new AmazonSimpleDBClient(new PropertiesCredentials(new File(propFile)));
		} catch (FileNotFoundException e) {
			throw new DAOException("The file " + propFile + " could not be found.");
		} catch (IllegalArgumentException e) {
			throw new DAOException("The file " + propFile + " does not contain the keys required to access the database.");
		} catch (IOException e) {
			throw new DAOException("The file " + propFile + " could not be read.");
		}
	}
	
	public void disconnect() {
		sdb.deleteAttributes(new DeleteAttributesRequest().withDomainName("gameStates"));
	}
	
	public String[] getUsers(boolean loggedIn) {
		List<String> users = new LinkedList<String>();

		String query = "SELECT u_id FROM user WHERE logged_in=\"" + loggedIn + "\"";
		SelectRequest sr = new SelectRequest(query);
		for (Item item : sdb.select(sr).getItems()) {
			List<Attribute> attrs = item.getAttributes();
			users.add(attrs.get(0).getValue());
		}
		return users.toArray(new String[1]);
	}
	
	public String getUserLoggedIn(String u_id) {		
		String query = "SELECT logged_in FROM user WHERE u_id=\"" + u_id + "\"";
		SelectRequest sr = new SelectRequest(query);
		List<Item> items = sdb.select(sr).getItems();
		if (items.isEmpty()) {
			return "";
		}
		
		List<Attribute> attrs = items.get(0).getAttributes();
		String loggedIn = (String) attrs.get(0).getValue();
		
		return loggedIn;
	}
	
	public void loginUser(String u_id) {
		PutAttributesRequest par = new PutAttributesRequest();
		par.setDomainName("user");
		par.setItemName(u_id);
		List<ReplaceableAttribute> attr = new LinkedList<ReplaceableAttribute>();
		attr.add(new ReplaceableAttribute().withName("u_id").withValue(u_id));
		attr.add(new ReplaceableAttribute().withName("logged_in").withValue("true"));
		par.setAttributes(attr);
		sdb.putAttributes(par);
	}
	
	public void logoutUser(String u_id) {
		
	}
	
	public void addGroup(int g_id, int e_id, int turn_id) {
		PutAttributesRequest par = new PutAttributesRequest();
		par.setDomainName("game");
		par.setItemName(g_id + "");
		List<ReplaceableAttribute> attr = new LinkedList<ReplaceableAttribute>();
		attr.add(new ReplaceableAttribute().withName("g_id").withValue(g_id + ""));
		attr.add(new ReplaceableAttribute().withName("e_id").withValue(e_id + ""));
		attr.add(new ReplaceableAttribute().withName("turn_id").withValue(turn_id + ""));
		par.setAttributes(attr);
		sdb.putAttributes(par);
	}
	
	public String[] getGroupMembers(int g_id) {
		List<String> members = new LinkedList<String>();

		String query = "SELECT u_id FROM player WHERE g_id=\"" + g_id + "\"";
		SelectRequest sr = new SelectRequest(query);
		for (Item item : sdb.select(sr).getItems()) {
			List<Attribute> attrs = item.getAttributes();
			members.add(attrs.get(0).getValue());
		}
		return members.toArray(new String[1]);
	}
	
	public void setEventID(int g_id, int e_id) {
		
	}
	
	public int getEventID(int g_id) {
		String query = "SELECT e_id FROM game WHERE g_id=\"" + g_id + "\"";
		SelectRequest sr = new SelectRequest(query);
		List<Item> items = sdb.select(sr).getItems();
		if (items.isEmpty()) {
			return -1;
		}
		
		List<Attribute> attrs = items.get(0).getAttributes();
		int e_id = Integer.parseInt(attrs.get(0).getValue());
		
		return e_id;
	}
	
	public void addGameDeck(int g_id, List<String> deck) {
		
	}
	
	public List<String> removeGameDeck(int g_id) {
		return null;
	}
	
	public void setPlayerHand(String u_id, List<String> cards) {
		// put cards in player_hand for u_id
	}
	
	public int getGameTurnID(int g_id) {
		return -99;
	}
	
	public void setGameTurnID(int g_id, int turn_id) {
		
	}
	
	public String getCardInPlayerHand(String u_id, String c_id) {
		return null;
	}
	
	public int getPlayerHandSize(String u_id) {
		return -1;
	}
	
	public int getGameDeckSize(int g_id) {
		return -1;
	}
	
	public String removeTopCardFromDeck(int g_id) {
		return null;
	}
	
	public void addCardToPlayerHand(String u_id, String c_id) {
		
	}
	
	public String removeCardFromPlayerHand(String u_id, String c_id) {
		return null;
	}
	
	public void addCardToPlayerPairs(String u_id, String c_id) {
		
	}
}
